package com.example.buyticketlishi.RetrufitHelp;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.example.baselibrary.Base.MyApplication;
import com.example.baselibrary.Bean.LiShiPointListResultBean;
import com.example.baselibrary.Bean.LiShifindLocationResultBean;
import com.example.baselibrary.Bean.LiShifindTerminalNameResultBean;
import com.example.baselibrary.Utils.Customer;
import com.example.baselibrary.Utils.SPUtils;
import com.example.baselibrary.Utils.StringUtil;
import com.example.buyticketlishi.bean.GetTravelAgencyListRequsetBean;
import com.example.buyticketlishi.bean.GetTravelAgencyListResultBean;
import com.example.buyticketlishi.bean.IpResult;
import com.example.buyticketlishi.bean.JJHCheckResultBean;
import com.example.buyticketlishi.bean.JJHCheckResultOtherBean;
import com.example.buyticketlishi.bean.JJHCheckTicketBean;
import com.example.buyticketlishi.bean.JJHCheckTicketOtherBean;
import com.example.buyticketlishi.bean.JJHCheckTicketOtherSecondBean;
import com.example.buyticketlishi.bean.JJHFindShipListRequestBean;
import com.example.buyticketlishi.bean.JJHFindShipListResultBean;
import com.example.buyticketlishi.bean.JJHLoginBean;
import com.example.buyticketlishi.bean.JJHLoginResult;
import com.example.buyticketlishi.bean.JJHPointListResultBean;
import com.example.buyticketlishi.bean.JJHSealTicketForTitleResultBean;
import com.example.buyticketlishi.bean.JJHSealTicketImforTitleBean;
import com.example.buyticketlishi.bean.JJHSealTitleBean;
import com.example.buyticketlishi.bean.JJHaddOrRelieveRequsetBean;
import com.example.buyticketlishi.bean.JJHaddOrRelieveResultBean;
import com.example.buyticketlishi.bean.JJHfindPointRequestBean;
import com.example.buyticketlishi.bean.JJHfindPointResultBean;
import com.example.buyticketlishi.bean.JJHfindRowboatOrderListRequestBean;
import com.example.buyticketlishi.bean.JJHfindRowboatOrderListResultBean;
import com.example.buyticketlishi.bean.JJHfindTerminalNameRequestBean;
import com.example.buyticketlishi.bean.JJHfindTerminalNameResultBean;
import com.example.buyticketlishi.bean.JJHgetAccessTokenByRefreshTokenBean;
import com.example.buyticketlishi.bean.JJHhandSetPayRequsetBean;
import com.example.buyticketlishi.bean.JJHhandSetPayResultBean;
import com.example.buyticketlishi.bean.JJHhandSetRequestBean;
import com.example.buyticketlishi.bean.JJHhandSetResultBean;
import com.example.buyticketlishi.bean.JJHterminalComputerRequestBean;
import com.example.buyticketlishi.bean.JJHterminalComputerResultBean;
import com.example.buyticketlishi.bean.LSVemPayOrderRequestBean;
import com.example.buyticketlishi.bean.LSVemPayOrderResultBean;
import com.example.buyticketlishi.bean.LiShihandSetReserveProductRequsetBean;
import com.example.buyticketlishi.bean.LiShihandSetReserveProductResultBean;
import com.example.buyticketlishi.bean.RzSelectListRequsetBean;
import com.example.buyticketlishi.bean.RzSelectListResultBean;
import com.example.buyticketlishi.bean.RzfindTimeControlListRequestBean;
import com.example.buyticketlishi.bean.RzfindTimeControlListResultBean;
import com.example.buyticketlishi.bean.RzhandSetResultBean;
import com.example.buyticketlishi.bean.TicketInfo;
import com.example.buyticketlishi.bean.TicketResult;
import com.example.buyticketlishi.bean.TicketResultNew;
import com.example.buyticketlishi.bean.checkOrderOnlineRequsetBean;
import com.example.buyticketlishi.bean.checkOrderOnlineResultBean;
import com.example.buyticketlishi.view.ac.LoginActivity;


import java.io.File;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * RetrofitClient
 * Created by Tamic on 2016-06-15.
 * {@link # https://github.com/NeglectedByBoss/RetrofitClient}
 */
public class RetrofitClient {

    private static final int DEFAULT_TIMEOUT = 10;
    private BaseApiService apiService;
    private static OkHttpClient okHttpClient;
    private static String baseUrl = Customer.LiSHI_BASE_URL;



    //    public static String baseUrl = BaseConfig.JJH_Base_URL;
    private static Context mContext;
    private static RetrofitClient sNewInstance;

    private static Retrofit retrofit;
    private Cache cache = null;
    private File httpCacheDirectory;


    private static Retrofit.Builder builder =
            new Retrofit.Builder()
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .baseUrl(baseUrl);
    private static OkHttpClient.Builder httpClient =
            new OkHttpClient.Builder()
                    .addNetworkInterceptor(
                            new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.HEADERS))
                    .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);


    private static class SingletonHolder {
        private static RetrofitClient INSTANCE = new RetrofitClient(
                mContext);
    }

    public static RetrofitClient getInstance(Context context) {
        if (context != null) {
            mContext = context;
        }
        return SingletonHolder.INSTANCE;
    }

    public static RetrofitClient getInstance(Context context, String url) {
        if (context != null) {
            mContext = context;
        }

        return new RetrofitClient(context, url);
    }

    public static RetrofitClient getInstance(Context context, String url, Map<String, String> headers) {
        if (context != null) {
            mContext = context;
        }
        return new RetrofitClient(context, url, headers);
    }

    private RetrofitClient() {

    }

    private RetrofitClient(Context context) {

        this(context, baseUrl, null);
    }

    private RetrofitClient(Context context, String url) {

        this(context, url, null);
    }

    private RetrofitClient(Context context, String url, Map<String, String> headers) {

        if (TextUtils.isEmpty(url)) {
            url = baseUrl;
        }

        if (httpCacheDirectory == null) {
            httpCacheDirectory = new File(mContext.getCacheDir(), "tamic_cache");
        }

        try {
            if (cache == null) {
                cache = new Cache(httpCacheDirectory, 10 * 1024 * 1024);
            }
        } catch (Exception e) {
            Log.e("OKHttp", "Could not create http cache", e);
        }
        okHttpClient = new OkHttpClient.Builder()
                .addNetworkInterceptor(
                        new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .cookieJar(new NovateCookieManger(context))
                .cache(cache)
                .addInterceptor(new BaseInterceptor(headers))
                .addInterceptor(new CaheInterceptor(context))
                .addNetworkInterceptor(new CaheInterceptor(context))
                .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .connectionPool(new ConnectionPool(8, 15, TimeUnit.SECONDS))
                // 这里你可以根据自己的机型设置同时连接的个数和时间，我这里8个，和每个保持时间为10s
                .build();
        retrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(url)
                .build();

    }

    /**
     * ApiBaseUrl
     *
     * @param newApiBaseUrl
     */
    public static void changeApiBaseUrl(String newApiBaseUrl) {
        baseUrl = newApiBaseUrl;
        builder = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl);
    }

    /**
     * addcookieJar
     */
    public static void addCookie() {
        okHttpClient.newBuilder().cookieJar(new NovateCookieManger(mContext)).build();
        retrofit = builder.client(okHttpClient).build();
    }

    /**
     * ApiBaseUrl
     *
     * @param newApiHeaders
     */
    public static void changeApiHeader(Map<String, String> newApiHeaders) {

        okHttpClient.newBuilder().addInterceptor(new BaseInterceptor(newApiHeaders)).build();
        builder.client(httpClient.build()).build();
    }

    /**
     * create BaseApi  defalte ApiManager
     *
     * @return ApiManager
     */
    public RetrofitClient createBaseApi() {
        if (apiService == null) {
            apiService = create(BaseApiService.class);
        }
        return this;
    }

    /**
     * create you ApiService
     * Create an implementation of the API endpoints defined by the {@code service} interface.
     */
    public <T> T create(final Class<T> service) {
        if (service == null) {
            throw new RuntimeException("Api service is null!");
        }
        return retrofit.create(service);
    }

    public Subscription getData(Subscriber<IpResult> subscriber, String ip) {
        return apiService.getData(ip)
                .compose(schedulersTransformer())
                .compose(transformer())
                .subscribe(subscriber);
    }

    public Subscription get(String url, Map parameters, Subscriber<IpResult> subscriber) {

        return apiService.executeGet(url, parameters)
                .compose(schedulersTransformer())
                .compose(transformer())
                .subscribe(subscriber);
    }

    public void post(String url, Map<String, String> parameters, Subscriber<ResponseBody> subscriber) {
        apiService.executePost(url, parameters)
                .compose(schedulersTransformer())
                .compose(transformer())
                .subscribe(subscriber);
    }

    public Subscription json(String url, RequestBody jsonStr, Subscriber<IpResult> subscriber) {

        return apiService.json(url, jsonStr)
                .compose(schedulersTransformer())
                .compose(transformer())
                .subscribe(subscriber);
    }

    public Subscription CheckTicket(TicketInfo jsonStr, Subscriber<TicketResult> subscriber) {
        createBaseApi();
        return apiService.TicketCheck(jsonStr)
                .compose(schedulersTransformer())
                .compose(transformer())
                .subscribe(subscriber);
    }

    public Subscription TicketCheckNew(TicketInfo jsonStr, Subscriber<TicketResultNew> subscriber) {
        createBaseApi();
        return apiService.TicketCheckNew(jsonStr)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }

    // 金鸡湖 登陆
    public Subscription JJHLogin(JJHLoginBean jjhLoginBean, Subscriber<JJHLoginResult> subscriber) {
        createBaseApi();
        return apiService.JJHLogin(jjhLoginBean)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }

    // 金鸡湖 检票接口
    public Subscription JJHCheckTicket(JJHCheckTicketBean jjhCheckTicketBean, Subscriber<JJHCheckResultBean> subscriber) {
        createBaseApi();
        return apiService.JJHCheckTicket(jjhCheckTicketBean)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }

    //日照馆 等其他 接口   检票
    public Subscription JJHCheckTicketOther(JJHCheckTicketOtherBean jjhCheckTicketBean, Subscriber<JJHCheckResultOtherBean> subscriber) {
        createBaseApi();
        return apiService.JJHCheckTicketOther(jjhCheckTicketBean)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }

    //检票成功后，有些需要人工二次确认
    public Subscription ticketCheckerConfirmByCellPhone(JJHCheckTicketOtherSecondBean jjhCheckTicketBean, Subscriber<JJHCheckResultOtherBean> subscriber) {
        createBaseApi();
        return apiService.ticketCheckerConfirmByCellPhone(jjhCheckTicketBean)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }


    // 金鸡湖 获取买票title
    public Subscription JJHSealTicketTitle(Subscriber<JJHSealTitleBean> subscriber) {
        createBaseApi();
        return apiService.JJHSealTicketTitle()
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }

    // 金鸡湖 获取买票title  list
    public Subscription JJHSealTicketList(JJHSealTicketImforTitleBean jjhSealTicketImforTitleBean, Subscriber<JJHSealTicketForTitleResultBean> subscriber) {
        createBaseApi();
        return apiService.JJHSealTicketList(jjhSealTicketImforTitleBean)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }

    //  立式 获取买票title  list
    public Subscription findUserSaleProduct(JJHSealTicketImforTitleBean jjhSealTicketImforTitleBean, Subscriber<JJHSealTicketForTitleResultBean> subscriber) {
        createBaseApi();
        return apiService.findUserSaleProduct(jjhSealTicketImforTitleBean)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }

    // 金鸡湖 获取时段信息
    public Subscription JJHfindShipRouteShiftsList(JJHFindShipListRequestBean findShipListRequestBean, Subscriber<JJHFindShipListResultBean> subscriber) {
        createBaseApi();
        return apiService.JJHfindShipRouteShiftsList(findShipListRequestBean)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }

    // 金鸡湖 订单创建
    public Subscription JJHhandSetReserveProduct(JJHhandSetRequestBean jjHhandSetRequestBean, Subscriber<JJHhandSetResultBean> subscriber) {
        createBaseApi();
        return apiService.JJHhandSetReserveProduct(jjHhandSetRequestBean)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }

    // 日照 订单创建
    public Subscription RZhandSetReserveProduct(JJHhandSetRequestBean jjHhandSetRequestBean, Subscriber<RzhandSetResultBean> subscriber) {
        createBaseApi();
        return apiService.RZhandSetReserveProduct(jjHhandSetRequestBean)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }


    // 立式 订单创建
    public Subscription LiShihandSetReserveProduct(LiShihandSetReserveProductRequsetBean jjHhandSetRequestBean, Subscriber<LiShihandSetReserveProductResultBean> subscriber) {
        createBaseApi();
        return apiService.LiShihandSetReserveProduct(jjHhandSetRequestBean)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }

    // 立式 自助机支付
    public Subscription LiShivemPayOrder(LSVemPayOrderRequestBean jjHhandSetRequestBean, Subscriber<LSVemPayOrderResultBean> subscriber) {
        createBaseApi();
        return apiService.LiShivemPayOrder(jjHhandSetRequestBean)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }


    // 金鸡湖 支付订单
    public Subscription JJHhandSetPayOrder(JJHhandSetPayRequsetBean jjHhandSetPayRequsetBean, Subscriber<JJHhandSetPayResultBean> subscriber) {
        createBaseApi();
        return apiService.JJHhandSetPayOrder(jjHhandSetPayRequsetBean)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }

    // 金鸡湖 订单轮询查看
    public Subscription checkOrderOnlinePayStatus(checkOrderOnlineRequsetBean jjHhandSetPayRequsetBean, Subscriber<checkOrderOnlineResultBean> subscriber) {
        createBaseApi();
        return apiService.checkOrderOnlinePayStatus(jjHhandSetPayRequsetBean)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }

    // 金鸡湖 获取景点名称列表
    public Subscription JJHgetScenicSpotBusinessList(Subscriber<LiShiPointListResultBean> subscriber) {
        createBaseApi();
        return apiService.JJHgetScenicSpotBusinessList()
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }

    // 金鸡湖 刷新token
    public Subscription JJHgetAccessTokenByRefreshToken(String refreshToken, Subscriber<JJHgetAccessTokenByRefreshTokenBean> subscriber) {
        createBaseApi();
        return apiService.JJHgetAccessTokenByRefreshToken(refreshToken)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }

    // 金鸡湖 获取定位名称列表
    public Subscription JJHfindPointSelect(JJHfindPointRequestBean jjHfindPointRequestBean, Subscriber<LiShifindLocationResultBean> subscriber) {
        createBaseApi();
        return apiService.JJHfindPointSelect(jjHfindPointRequestBean)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }

    // 金鸡湖 获取终端名称列表
    public Subscription JJHfindTerminalNameAndIdList(JJHfindTerminalNameRequestBean jjHfindPointRequestBean, Subscriber<LiShifindTerminalNameResultBean> subscriber) {
        createBaseApi();
        return apiService.JJHfindTerminalNameAndIdList(jjHfindPointRequestBean)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }

    // 金鸡湖 绑定接口
    public Subscription JJHaddOrRelieveTerminalBind(JJHaddOrRelieveRequsetBean jjHfindPointRequestBean, Subscriber<JJHaddOrRelieveResultBean> subscriber) {
        createBaseApi();
        return apiService.JJHaddOrRelieveTerminalBind(jjHfindPointRequestBean)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }

    // 金鸡湖 根据终端terminalComputerCpuId查询当前设备绑定终端信息
    public Subscription JJHfindTerminalInfoByComputerCpuId(JJHterminalComputerRequestBean jjHfindPointRequestBean, Subscriber<JJHterminalComputerResultBean> subscriber) {
        createBaseApi();
        return apiService.JJHfindTerminalInfoByComputerCpuId(jjHfindPointRequestBean)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }

    //金鸡湖项目 接口   订单查询 游船预订
    public Subscription JJHfindRowboatOrderList(JJHfindRowboatOrderListRequestBean jjHfindPointRequestBean, Subscriber<JJHfindRowboatOrderListResultBean> subscriber) {
        createBaseApi();
        return apiService.JJHfindRowboatOrderList(jjHfindPointRequestBean)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }

    //金鸡湖项目 接口   获取单位信息用于下拉框
    public Subscription JJHgetTravelAgencyList(GetTravelAgencyListRequsetBean jjHfindPointRequestBean, Subscriber<GetTravelAgencyListResultBean> subscriber) {
        createBaseApi();
        return apiService.JJHgetTravelAgencyList(jjHfindPointRequestBean)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }


    //日照科技馆 新添加接口    景区门票产品名称
    public Subscription RzfindScenicTicketSelectList(RzSelectListRequsetBean jjHfindPointRequestBean, Subscriber<RzSelectListResultBean> subscriber) {
        createBaseApi();
        return apiService.RzfindScenicTicketSelectList(jjHfindPointRequestBean)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }

    //日照科技馆 新添加接口   获取分时预约库存（按产品）
    public Subscription RzfindTimeControlListByScenicTicket(RzfindTimeControlListRequestBean jjHfindPointRequestBean, Subscriber<RzfindTimeControlListResultBean> subscriber) {
        createBaseApi();
        return apiService.RzfindTimeControlListByScenicTicket(jjHfindPointRequestBean)
                .compose(schedulersTransformer())
                .compose(transformerSelf())
                .subscribe(subscriber);
    }


    public void upload(String url, RequestBody requestBody, Subscriber<ResponseBody> subscriber) {
        apiService.upLoadFile(url, requestBody)
                .compose(schedulersTransformer())
                .compose(transformer())
                .subscribe(subscriber);
    }

    public void download(String url, final CallBack callBack) {
        apiService.downloadFile(url)
                .compose(schedulersTransformer())
                .compose(transformer())
                .subscribe(new DownSubscriber<ResponseBody>(callBack));
    }

    Observable.Transformer schedulersTransformer() {
        return new Observable.Transformer() {


            @Override
            public Object call(Object observable) {
                return ((Observable) observable).subscribeOn(Schedulers.io())
                        .unsubscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }

           /* @Override
            public Observable call(Observable observable) {
                return observable.subscribeOn(Schedulers.io())
                        .unsubscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }*/
        };
    }

    <T> Observable.Transformer<T, T> applySchedulers() {
        return (Observable.Transformer<T, T>) schedulersTransformer();
    }

    public <T> Observable.Transformer<BaseResponse<T>, T> transformer() {

        return new Observable.Transformer() {

            @Override
            public Object call(Object observable) {
                return ((Observable) observable).map(new HandleFuc<T>()).onErrorResumeNext(new HttpResponseFunc<T>());
            }
        };
    }

    public <T> Observable.Transformer<T, T> transformerSelf() {

        return new Observable.Transformer() {

            @Override
            public Object call(Object observable) {
                return ((Observable) observable).map(new HandleFucSelf()).onErrorResumeNext(new HttpResponseFunc<T>());
            }
        };
    }


    public <T> Observable<T> switchSchedulers(Observable<T> observable) {
        return observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    private static class HttpResponseFunc<T> implements Func1<Throwable, Observable<T>> {
        @Override
        public Observable<T> call(Throwable t) {
            return Observable.error(ExceptionHandle.handleException(t));
        }
    }

    private class HandleFuc<T> implements Func1<BaseResponse<T>, T> {
        @Override
        public T call(BaseResponse<T> response) {
            if (response.getInfo() == null) {
                throw new NullPointerException("返回info数据为空");
            }
            return response.getInfo();
        }
    }

    private class HandleFucSelf<T> implements Func1<T, T> {
        @Override
        public T call(T response) {
            if (response == null) {
                throw new NullPointerException("返回info数据为空");
            }
            return response;
        }
    }


    /**
     * /**
     * execute your customer API
     * For example:
     * MyApiService service =
     * RetrofitClient.getInstance(MainActivity.this).create(MyApiService.class);
     * <p>
     * RetrofitClient.getInstance(MainActivity.this)
     * .execute(service.lgon("name", "password"), subscriber)
     * * @param subscriber
     */

    public static <T> T execute(Observable<T> observable, Subscriber<T> subscriber) {
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);

        return null;
    }


    /**
     * DownSubscriber
     *
     * @param <ResponseBody>
     */
    class DownSubscriber<ResponseBody> extends Subscriber<ResponseBody> {
        CallBack callBack;

        public DownSubscriber(CallBack callBack) {
            this.callBack = callBack;
        }

        @Override
        public void onStart() {
            super.onStart();
            if (callBack != null) {
                callBack.onStart();
            }
        }

        @Override
        public void onCompleted() {
            if (callBack != null) {
                callBack.onCompleted();
            }
        }

        @Override
        public void onError(Throwable e) {
            if (callBack != null) {
                callBack.onError(e);
            }
        }

        @Override
        public void onNext(ResponseBody responseBody) {
            DownLoadManager.getInstance(callBack).writeResponseBodyToDisk(mContext, (okhttp3.ResponseBody) responseBody);

        }
    }

}
